package com.keliangliu.demo.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.stream.Stream;

public class DemoController {
    @DemoAutowired
    private DemoService demoService;

    private void setDemoService(DemoService demoService) {
        this.demoService = demoService;
    }

    public DemoService getDemoService() {
        return demoService;
    }


    public static void main(String[] args) {
        Class<DemoController> demoControllerClass = DemoController.class;
        Stream.of(demoControllerClass.getDeclaredFields()).forEach(field -> {
            DemoAutowired annotation = field.getAnnotation(DemoAutowired.class);
            if(annotation != null) {
                field.setAccessible(true);
                Class demoServiceClass = field.getClass();
                DemoService demoService = null;
                try {
                    demoService = (DemoService) demoServiceClass.getConstructor(null).newInstance();

                    field.set(demoControllerClass,demoService);
                    String methodName = field.getName().substring(0,1).toUpperCase() + field.getName().substring(1,field.getName().getBytes().length);
                    Method method = demoControllerClass.getMethod(methodName);
                    System.out.println();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        });


    }
}
